<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Controller\CommonController;
require_once 'Qiniu/autoload.php';
use \Qiniu\Auth;
use \Qiniu\Storage\UploadManager;
/**
 * APP版本设置
 */
class VersionController extends CommonController { 

	public function index() {
		$type = I('get.type', 'android');


		$putPolicy=array(
            'saveKey'=>'apk/$(etag).apk',
        );
        $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
        //$bucket = 'images';
        $bucket = 'whtv-apk';
        $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);


        $this->assign('uploadToken', $uploadToken);


		$v_model = M('FrontVersion');
		$map['type'] = $type;

		$total = $v_model->where($map)->count();
        $page = new \Think\Page($total, 100);
        $show = $page->show();

        $vList = $v_model->where($map)
                ->limit($page->firstRow .',' . $page->listRows)
                ->order('create_time DESC')
                ->select();   

        $this->assign('showpage', $show);
        $this->assign('vList', $vList);
        $this->assign('type', $type);
        $this->display();
	}

	public function add() {
		if(IS_POST) {
			$version = I('post.version');


			if($version) {
                $dataGet = I('post.');
				$model = M('FrontVersion');

				/*if($_FILES['download_url']['error'] == 0 && I('post.type')=='android') {
					$upload_path = $this->upload('download_url');
				}*/


				

				$dataGet['update_time'] = $dataGet['create_time'] = time();
				/*$upload_path &&  $dataGet['download_url'] = '/Uploads/' . $upload_path;*/
				if($model->create($dataGet)) {
					$ret = M('FrontVersion')->add($dataGet);
					if($ret) {
						$this->success('添加成功');
					} else {
						$this->error('添加失败');
					}

				} else {
					$this->error($model->getError());
				}
			} else {
				$this->error('版本号不能为空');
			}
		}
	}


	public function edit() {
		if(IS_POST) {
			$version = I('post.version');
			if($version) {
				$model = M('FrontVersion');

				/*if($_FILES['download_url']['error'] == 0 && I('post.type')=='android' ) {
					$upload_path = $this->upload('download_url');
					$upload_path && $_POST['download_url'] = '/Uploads/' . $upload_path;
				}*/

                $_POST['update_time'] = time();
                if($_POST['download_url']==""){
                	unset($_POST['download_url']);
                }

				if($model->create()) {
					$ret = M('FrontVersion')->save();
					if($ret) {
						$this->success('修改成功');
					} else {
						$this->error('修改失败');
					}

				} else {
					$this->error($model->getError());
				}
			} else {
				$this->error('版本号不能为空');
			}
		}
	}

	public function ajaxGetVersionInfo($id) {
        $model =M('FrontVersion');
        if($id){
            $info=$model->where('id='.$id)->find();
            if($info){
                $this->ajaxReturn($info);
            }else{
                $this->ajaxReturn(0);
            }
        }else{  
            return false;
        }
	}

	public function ajaxdelversion($id) {
        $model=M('FrontVersion');
        if($id){
        	$map['id'] = $id;
        	$ret = $model->where($map)->delete();
            if($ret){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }else{  
            return false;
        }		
	}

	Public function upload($name){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     300145728 ;// 设置附件上传大小
		$upload->exts      =     array('apk');// 设置附件上传类型
		$upload->rootPath  =     '/Uploads/'; // 设置附件上传根目录
		$upload->savePath  =     'Package/'; // 设置附件上传（子）目录
		// 上传文件
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			return $info[$name]['savepath'] . $info[$name]['savename'];
		}
	}


	private function versionCompare($versionA,$versionB) {
        if ($versionA>2147483646 || $versionB>2147483646) {
            throw new Exception('版本号,位数太大暂不支持!','101');
        }
        $dm = '.';
        $verListA = explode($dm, (string)$versionA);
        $verListB = explode($dm, (string)$versionB);

        $len = max(count($verListA),count($verListB));
        $i = -1;
        while ($i++<$len) {
            $verListA[$i] = intval(@$verListA[$i]);
            if ($verListA[$i] <0 ) {
                $verListA[$i] = 0;
            }
            $verListB[$i] = intval(@$verListB[$i]);
            if ($verListB[$i] <0 ) {
                $verListB[$i] = 0;
            }

            if ($verListA[$i]>$verListB[$i]) {
                return 1;
            } else if ($verListA[$i]<$verListB[$i]) {
                return -1;
            } else if ($i==($len-1)) {
                return 0;
            }
        }

    }


}